<?php
declare(strict_types=1);

namespace app\controller;

use app\service\CoordinateService;
use support\Request;
use support\Response;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("坐标互转")
 * @Apidoc\Group("http")
 */
class CoordinateController
{
    public function __construct(private CoordinateService $svc = new CoordinateService()) {}

    /**
     * @Apidoc\Title("坐标互转")
     * @Apidoc\Method("GET,POST")
     * @Apidoc\Url("/coord/convert")
     * @Apidoc\Param(name="lat", type="float", require=true, desc="纬度")
     * @Apidoc\Param(name="lon", type="float", require=true, desc="经度")
     * @Apidoc\Param(name="from", type="string", default="wgs84", desc="源坐标系")
     * @Apidoc\Param(name="to", type="string", default="bd09ll", desc="目标坐标系")
     * @Apidoc\Returned(name="ok", type="bool", desc="是否成功")
     * @Apidoc\Returned(name="from", type="string", desc="源坐标系")
     * @Apidoc\Returned(name="to", type="string", desc="目标坐标系")
     * @Apidoc\Returned(name="input", type="object", desc="入参回显")
     * @Apidoc\Returned(name="output", type="object", desc="转换结果", main=true)
     */
    public function convert(Request $request): Response
    {
        $lat = (float)($request->get('lat', $request->post('lat', 0)));
        $lon = (float)($request->get('lon', $request->post('lon', 0)));
        $from = strtolower((string)($request->get('from', $request->post('from', 'wgs84'))));
        $to = strtolower((string)($request->get('to', $request->post('to', 'bd09ll'))));

        $res = $this->svc->convert($lat, $lon, $from, $to);
        return json([
            'from' => $from,
            'to' => $to,
            'input' => ['lat' => $lat, 'lon' => $lon],
            'output' => $res,
        ]);
    }
}